package com.mrtan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mrtan.bean.Product;
import com.mrtan.bean.Supplier;
import com.mrtan.bean.SupplierProduct;
import com.mrtan.dto.MCDto;
import com.mrtan.mapper.ProductMapper;
import com.mrtan.mapper.SupplierMapper;
import com.mrtan.mapper.SupplierProductMapper;
import com.mrtan.service.SupplierService;
import com.mrtan.vo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 供应商
 *
 * @author 李聪
 * @version 2023/12/8
>>>>>>> origin/master
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Resource
    private SupplierMapper supplierMapper;
    @Resource
    private SupplierProductMapper supplierProductMapper;
    @Resource
    private ProductMapper productMapper;


    @Override
    public int addSupplier(SupplierVO supplierVO) {
        //供应商去重
        Supplier supplier = supplierMapper.selectByName(supplierVO.getName());
        if (supplier != null) {
            return -1;
        }
        int i = supplierMapper.insertSelective(BeanUtil.copyProperties(supplierVO, Supplier.class));
        return i;
    }

    @Override
    public List<SupplierProduct> selectAll(SupplierProductVO supplierProductVO) {
        return supplierProductMapper.selectAll(supplierProductVO);
    }

    /**
     * 查所有供应商
     *
     * @return {@link List}<{@link Supplier}>
     */

    @Override
    public List<Supplier> selectAllSupplier() {
        List<Supplier> suppliers = supplierMapper.selectAll();
        return suppliers;
    }

    /**
     * 供应商添加商品
     *
     * @param addSupplierProductVO
     * @return int
     */
    @Override
    @Transactional
    public int addProduct(AddSupplierProductVO addSupplierProductVO) {
        ProductWhereVO productWhereVO = new ProductWhereVO();
        productWhereVO.setBrand(addSupplierProductVO.getBrand());
        productWhereVO.setModel(addSupplierProductVO.getModel());
        productWhereVO.setName(addSupplierProductVO.getName());
        //查询所有产品有无该产品
        Product select = productMapper.select(productWhereVO);
        int insert =0;
        if (select != null) {
            //查看所有供应商产品有无该产品
            SupplierProduct ishave = supplierProductMapper.ishave(select.getId(), addSupplierProductVO.getSupplier());
            if (ishave != null) {
                return -1;
            }
            addSupplierProductVO.setProduct(select.getId());
            insert = this.insert(addSupplierProductVO);
        } else {
            //没有的话就add一个产品
            int i = productMapper.addProduct(productWhereVO);
            if(i>0){
                Product product = productMapper.select(productWhereVO);
                addSupplierProductVO.setProduct(product.getId());
                 insert = this.insert(addSupplierProductVO);
            }
        }

        return insert;
    }

    private int insert(AddSupplierProductVO addSupplierProductVO) {
        return supplierProductMapper.addProduct(addSupplierProductVO);
    }


    @Override
    public List<Supplier> findAllSupplier(SupplierVO supplierVO) {
        List<Supplier> suppliers = supplierMapper.allBySupplierVO(supplierVO);
        return suppliers;
    }

    @Override
    public int romove(int productid, int supplierid) {
        SupplierProduct ishave = supplierProductMapper.ishave(productid, supplierid);
        if(ishave==null){
            return -1;
        }
       int i= supplierProductMapper.remove(productid,supplierid);
        return i;
    }

    @Override
    public int updateproduct(UpdateSupplierProduct updateSupplierProduct) {
        SupplierProduct ishave = supplierProductMapper.ishave(updateSupplierProduct.getProductId(), updateSupplierProduct.getSupplierId());
        if(ishave==null){
            return -1;
        }
        int i= supplierProductMapper.update(updateSupplierProduct);
        return i;
    }

    @Override
    public void updateQuantity(PurchaseOrderVO purchaseOrderVO) {
        List<ProductInfo> productInfo = purchaseOrderVO.getProductInfo();
        for (ProductInfo info : productInfo) {
            Integer quantity = info.getQuantity();//采购数量
            Integer product = info.getProduct();  // 采购产品id
            Integer hasQuantity = supplierProductMapper.selectQuantityById(product);
            Integer i = hasQuantity - quantity;  //最终数量
            supplierProductMapper.updateQuantity(i,product);
        }
    }

    @Override
    public int getSupplierCount() {
        int i = supplierMapper.selectSupplierCount();
        return i;
    }

    @Override
    public List<MCDto> getModelAndCount(Integer id) {
        List<MCDto> list = supplierProductMapper.selectProId(id);
        return list;
    }
}
